<?php

namespace app\admin\model\xilujob;

use think\Exception;
use think\Log;
use think\Model;
use traits\model\SoftDelete;
use function fast\array_get;

class Job extends Model
{

    use SoftDelete;

    const STATUS_DISABLED = 0;//下架
    const STATUS_ENABLED = 1;//上架
    const STATUS_AUDITING = 2;//待审核
    const STATUS_FAILED = 3;//未过审

    // 表名
    protected $name = 'xilujob_job';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'publisher_type_text',
        'type_text',
        'salary_type_text',
        'refreshtime_text',
        'status_text',
        'black_text'
    ];
    

    
    public function getPublisherTypeList()
    {
        return ['2' => __('Publisher_type 2'), '3' => __('Publisher_type 3')];
    }

    public function getTypeList()
    {
        return ['1' => __('Type 1'), '2' => __('Type 2')];
    }

    public function getSalaryTypeList()
    {
        return [' 0' => __('Salary_type  0'), ' 1' => __('Salary_type  1'), '2' => __('Salary_type 2'), '3' => __('Salary_type 3')];
    }

    public function getStatusList()
    {
        return [' 2' => __('Status  2'),'1' => __('Status 1'), '0' => __('Status 0'),   ' 3' => __('Status  3')];
    }

    public function getBlackList()
    {
        return ['0' => __('Black 0'), '1' => __('Black 1')];
    }


    public function getPublisherTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['publisher_type']) ? $data['publisher_type'] : '');
        $list = $this->getPublisherTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
        $list = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getSalaryTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['salary_type']) ? $data['salary_type'] : '');
        $list = $this->getSalaryTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getRefreshtimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['refreshtime']) ? $data['refreshtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getBlackTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['black']) ? $data['black'] : '');
        $list = $this->getBlackList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    /**
     * 关联查询 JobCategory
     * @return \think\model\relation\BelongsTo
     */
    public function jobCategory() {
        return $this->belongsTo('JobCategory','job_category_id','id',[],'LEFT')->setEagerlyType(0);
    }

    public function enterpriseinfo() {
        return $this->belongsTo('EnterpriseInfo', 'publisher_id','company_id')->setEagerlyType(0);
    }

    public function city() {
        return $this->belongsTo('Area','city_id');
    }

    public function benefits() {
        return $this->belongsToMany('Benefit',JobBenefit::class,'benefit_id','job_id');
    }

    public function jobInfo() {
        return $this->hasOne('JobInfo', 'job_id','id');
    }

    public function locations() {
        return $this->hasMany('JobLocation', 'job_id');
    }


    //读取jobInfo的属性
    public function getResponsibilityAttr() {
        return $this->job_info->responsibility;
    }
    public function getRequirementAttr() {
        return $this->job_info->requirement;
    }
    public function getEnvironmentImagesAttr() {
        return $this->job_info->environment_images;
    }
    public function getContactPersonAttr() {
        return $this->job_info->contact_person;
    }
    public function getContactPhoneAttr() {
        return $this->job_info->contact_phone;
    }
    public function getContactEmailAttr() {
        return $this->job_info->contact_email;
    }
    public function getFailedReasonAttr() {
        return $this->job_info->failed_reason;
    }

}
